package com.news.news.web;

import com.github.pagehelper.PageInfo;
import com.news.news.dao.RoleDao;
import com.news.news.modle.Admin;
import com.news.news.modle.Menu;
import com.news.news.modle.Role;
import com.news.news.service.AdminService;
import com.news.news.service.RoleService;
import com.news.news.util.CommonResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * Created by 罗铠威 on 2023/8/29 8:19
 * type:
 */
@RestController
@RequestMapping(path = "/admin/RoleCtl")
public class RoleController {
    @Resource
    RoleService  roleService;

    /**
     * 获取角色数据
     * @param role
     * @return
     */
    @RequestMapping(value = "/list")
    public CommonResult list(@RequestBody Role role) {
        PageInfo<Role> rolePageInfo = roleService.adminList(role);
        CommonResult commonResult=new CommonResult(200,rolePageInfo,"成功");

        return commonResult;
    }

    /**
     * 获取新增的菜单列表
     * @return
     */
    @RequestMapping(value = "/findMenus")
    public CommonResult findMenus() {
     ArrayList<Menu> menus= roleService.findMenus();
        CommonResult commonResult=new CommonResult(200,menus,"成功");

        return commonResult;
    }

    /**
     * 保存新增角色信息
     * @return
     */
    @RequestMapping(value = "/saveRole")
    public CommonResult saveRole(@RequestBody Role role, @RequestHeader("adminToken") String token) {
        if (role.getId() != null) {//【修改】
            roleService.updateRole(role,token);
        } else {//【新增】
            roleService.saveRole(role,token);
        }
        CommonResult commonResult = new CommonResult(200, null, "成功");

        return commonResult;
    }

    /**
     * 编辑查找信息--通过ID
     * @param id
     * @return
     */
    @RequestMapping(value = "/findRoleById")
    public CommonResult findRoleById(Integer id) {
        Role role = roleService.findRoleById(id);
        CommonResult commonResult = new CommonResult(200, role, "成功");
        return commonResult;
    }
    @RequestMapping(value = "/deleteRole")
    public CommonResult deleteRole(Integer id) {
        roleService.deleteRole(id);
        CommonResult commonResult = new CommonResult(200, null, "成功");
        return commonResult;
    }

}
